//
//  LBViewController.m
//  LBMiddleExpandLoopView
//
//  Created by liubo on 09/21/2023.
//  Copyright (c) 2023 liubo. All rights reserved.
//

#import "LBViewController.h"
#import <LBMiddleExpandLoopView/LBMiddleExpandLoopView.h>
#import "LBMiddleExdpandLoopViewCell.h"
#import "LBMiddleIndicatorCell.h"

@interface LBViewController () <LBMiddleExpandLoopViewDelegate, LBMiddleExpandLoopViewDelegate>

@property (nonatomic, strong) LBMiddleExpandLoopView *loopView;

@property (nonatomic, strong) LBMiddleExpandLoopView *indicatorView;

@property (nonatomic, assign) BOOL  isScrollCard;
@property (nonatomic, assign) BOOL  isScrollIcon;

@end

@implementation LBViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.loopView];
    [self.view addSubview:self.indicatorView];
    [self.loopView reloadData];
    [self.indicatorView reloadData];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - LBMiddleExpandViewDelegate, LBMiddleExpandViewDataSource

#pragma mark - lazy load
- (NSInteger)numberOfCellsInCycleScrollView:(LBMiddleExpandLoopView *)cycleScrollView {
    return 6;
}

- (LBMiddleExdpandLoopViewCell *)cycleScrollView:(LBMiddleExpandLoopView *)cycleScrollView cellForViewAtIndex:(NSInteger)index {
    
    if (cycleScrollView == self.loopView) {
        LBMiddleExdpandLoopViewCell *cell = [cycleScrollView dequeueReusableCellWithIdentifier:@"333"];
        if (!cell) {
            cell = [LBMiddleExdpandLoopViewCell new];
            cell.layer.cornerRadius = 10;
            cell.layer.masksToBounds = YES;
            cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
        }
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",index + 1]];
        return cell;
    }
    LBMiddleIndicatorCell *cell = [cycleScrollView dequeueReusableCellWithIdentifier:@"8888"];
    [cell updateWithModel:[NSString stringWithFormat:@"%ld", index]];
    return cell;
}

- (CGSize)sizeForCellInCycleScrollView:(LBMiddleExpandLoopView *)cycleScrollView {
    
    if (cycleScrollView == self.loopView) {
        return CGSizeMake(260, 200);
    }
    return CGSizeMake(50, 50);
}

- (void)cycleScrollView:(LBMiddleExpandLoopView *)cycleScrollView didScrollCellToIndex:(NSInteger)index {
    if (cycleScrollView.scrollView.isTracking) return;

}

- (void)cycleScrollView:(LBMiddleExpandLoopView *)cycleScrollView willBeginDragging:(UIScrollView *)scrollView {
    if (cycleScrollView == self.loopView) {
        self.isScrollCard = YES;
    }else if (cycleScrollView == self.indicatorView) {
        self.isScrollIcon = YES;
    }
}


- (void)cycleScrollView:(LBMiddleExpandLoopView *)cycleScrollView didScroll:(UIScrollView *)scrollView {
    if (cycleScrollView == self.loopView) {
        if (self.isScrollIcon) return;
        if (!self.isScrollCard) return;
        CGFloat ratio = scrollView.contentOffset.x / scrollView.contentSize.width;
        CGPoint offset = CGPointMake(self.indicatorView.scrollView.contentSize.width * ratio, 0);
        [self.indicatorView.scrollView setContentOffset:offset animated:NO];
    }else if (cycleScrollView == self.indicatorView) {
        if (self.isScrollCard) return;
        if (!self.isScrollIcon) return;
        CGFloat ratio = scrollView.contentOffset.x / scrollView.contentSize.width;
        CGPoint offset = CGPointMake(self.loopView.scrollView.contentSize.width * ratio, 0);
        [self.loopView.scrollView setContentOffset:offset animated:NO];
    }
}

- (void)cycleScrollView:(LBMiddleExpandLoopView *)cycleScrollView didEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (!decelerate) {
        if (cycleScrollView == self.loopView) {
            self.isScrollCard = NO;
        }else if (cycleScrollView == self.indicatorView) {
            self.isScrollIcon = NO;
        }
    }
}

- (void)cycleScrollView:(LBMiddleExpandLoopView *)cycleScrollView didEndDecelerating:(UIScrollView *)scrollView {
    if (cycleScrollView == self.loopView) {
        self.isScrollCard = NO;
    }else if (cycleScrollView == self.indicatorView) {
        self.isScrollIcon = NO;
    }
}


#pragma mark - 懒加载

- (LBMiddleExpandLoopView *)loopView {
    if (!_loopView) {
        _loopView = [LBMiddleExpandLoopView new];
        _loopView.dataSource = self;
        _loopView.delegate = self;
        [_loopView registerClass:[LBMiddleExdpandLoopViewCell class] forViewReuseIdentifier:@"333"];
//        _cycleScrollView.isAutoScroll = NO;
        _loopView.isInfiniteLoop = NO;
        _loopView.leftRightMargin = 40;
        _loopView.topBottomMargin = 40;
        _loopView.frame = CGRectMake(0, 100, CGRectGetWidth(self.view.bounds), 200);
    }
    return _loopView;
}

- (LBMiddleExpandLoopView *)indicatorView
{
    if (!_indicatorView) {
        _indicatorView = [LBMiddleExpandLoopView new];
        _indicatorView.dataSource = self;
        _indicatorView.delegate = self;
        [_indicatorView registerClass:[LBMiddleIndicatorCell class] forViewReuseIdentifier:@"8888"];
//        _cycleScrollView.isAutoScroll = NO;
        _indicatorView.isInfiniteLoop = NO;
        _indicatorView.leftRightMargin = 5;
        _indicatorView.topBottomMargin = 5;
        _indicatorView.frame = CGRectMake(0, 400, CGRectGetWidth(self.view.bounds), 50);
    }
    return _indicatorView;
}

@end
